package com.api.user.controller;

import com.api.common.EmailUtil;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.user.model.*;
import com.api.user.service.CompanionGameService;
import com.api.user.service.CompanionOrderService;
import com.api.user.service.CompanionService;
import com.api.user.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.api.core.annotation.PowerEnable;
import io.swagger.annotations.*;


/**
 * Created by wanghuiwen on 2021/04/02.
 */
@PowerEnable(name = "陪玩", url = "/companion")
@Api(value = "陪玩", tags = {"陪玩"})
@RestController
@RequestMapping("/companion")
public class CompanionController extends Ctrl {
    @Resource
    private CompanionService companionService;
    @Resource
    private CompanionGameService companionGameService;

    @Resource
    private UserInfoService userInfoService;
    @ApiOperation(value = "用户申请陪玩", tags = {"陪玩"}, notes = "用户申请陪玩")
    @PostMapping(value = "/apply", name = "用户申请陪玩")
    public Result apply(@ApiParam CompanionGame companionGame) {

        return companionService.apply(companionGame);
    }


    @ApiOperation(value = "完善陪玩信息", tags = {"陪玩"}, notes = "完善陪玩信息")
    @PostMapping(value = "/update", name = "完善陪玩信息")
    public Result update(@RequestBody CompanionPerfect companionPerfect) {
        companionService.perfect(companionPerfect);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "陪玩审核", tags = {"陪玩"}, notes = "陪玩审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companionId", required = true, value = "陪玩id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "reason", required = false, value = "不通过的原因", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "examine", required = true, value = "审核状态", dataType = "Long", paramType = "query")
    })
    @PostMapping(value = "/examine", name = "陪玩审核")
    public Result examine(@RequestParam Long companionId, @RequestParam(required = false) String reason, @RequestParam Integer examine) {
        companionService.examine(companionId, reason, examine);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "下陪玩订单", tags = {"陪玩"}, notes = "下陪玩订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companionGameId", required = true, value = "陪玩id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "hours", required = true, value = "买几个小时", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", required = true, value = "开始的时间", dataType = "Long", paramType = "query")
    })
    @PostMapping(value = "/buy", name = "下陪玩订单")
    public Result buy(@RequestParam Long companionGameId, @RequestParam Integer hours, @RequestParam Long startTime, Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();

        Result result = companionService.buy(userId, companionGameId, hours, startTime);
        if(result.getCode() == 200){
            CompanionGame companionGame = companionGameService.findById(companionGameId);
            UserInfo info = userInfoService.findById(companionGame.getCompanionId());
            EmailUtil.gmailSender(info.getEmail(),"Someone has requested your service ! Are you ready to unleash your skill ? ",null);
        }
        return result;
    }

    @ApiOperation(value = "同意拒绝订单", tags = {"陪玩"}, notes = "同意拒绝订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companionOrderId", required = true, value = "陪玩订单ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "agree", required = true, value = "是否同意 true/false", dataType = "String", paramType = "query")
    })
    @PostMapping(value = "/order/agree", name = "同意拒绝订单")
    public Result orderAgree(@RequestParam Long companionOrderId, @RequestParam Boolean agree, Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();
        return companionService.agree(userId, companionOrderId, agree);
    }

    @ApiOperation(value = "完成订单", tags = {"陪玩"}, notes = "完成订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companionOrderId", required = true, value = "陪玩订单ID", dataType = "Long", paramType = "query")
    })
    @PostMapping(value = "/order/complete", name = "完成订单")
    public Result orderComplete(@RequestParam Long companionOrderId) {


        return companionService.orderComplete(companionOrderId);
    }


    @ApiOperation(value = "评论陪玩订单", tags = {"陪玩"}, notes = "评论陪玩订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companionOrderId", required = true, value = "陪玩订单ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "evaluate", required = true, value = "评价", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "score", required = true, value = "分数", dataType = "Long", paramType = "query")
    })
    @PostMapping(value = "/order/review", name = "评论陪玩订单")
    public Result orderReview(@RequestParam Long companionOrderId, @RequestParam String evaluate, @RequestParam BigDecimal score, Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();

        return companionService.orderReview(companionOrderId, evaluate, score, userId);
    }


    @ApiOperation(value = "陪玩订单统计", tags = {"陪玩"}, notes = "陪玩订单统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "陪玩订单ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "gameCategory", value = "游戏类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId",value = "下单用户", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "startTime",value = "开始时间", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "companionId",  value = "陪玩用户Id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", dataType = "Long", paramType = "query")
    })
    @PostMapping(value = "/statistics", name = "陪玩订单统计")
    public Result statistics(@RequestParam(defaultValue = "") Long orderId,
                             @RequestParam(defaultValue = "") String gameCategory,
                             @RequestParam(defaultValue = "") Long userId,
                             @RequestParam(defaultValue = "") Long startTime,
                             @RequestParam(defaultValue = "") Long endTime,
                             @RequestParam(defaultValue = "") Long companionId,
                             @RequestParam(defaultValue = "") Long state) {
        Map<String, Object> params = new HashMap<>();
        params.put("orderId",orderId);
        params.put("gameCategory",gameCategory);
        params.put("userId",userId);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("companionId",companionId);
        params.put("state",state);


        return companionService.statistics(params);
    }

    @ApiOperation(value = "陪玩详细信息", tags = {"陪玩"}, notes = "陪玩详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, value = "陪玩用户的ID", dataType = "Long", paramType = "query")
    })
    @PostMapping(value = "/detail", name = "陪玩详细信息")
    public Result detail(@RequestParam Long userId,@RequestParam(required = false) Boolean service, Authentication authentication) {
        Long fansId = getAuthUser(authentication).getId();

        Map<String, Object> companion = companionService.detail(userId, fansId,service);
        return ResultGenerator.genSuccessResult(companion);
    }

    @ApiOperation(value = "陪玩列表管理端", tags = {"陪玩"}, notes = "陪玩列表管理端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/list", name = "陪玩列表管理端")
    public Result list(
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "{}") String order,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {

        Map<String, Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("phone",phone);
        params.put("name",name);
        return companionService.list(params, order, page, size);
    }

    @ApiOperation(value = "陪玩列表用户端", tags = {"陪玩"}, notes = "陪玩列表用户端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/user/list", name = "陪玩列表用户端")
    public Result userList(
            @RequestParam(required = false) Long gameId,
            @RequestParam(required = false) String uid,
            @RequestParam(defaultValue = "{}") String order,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();
        Map<String, Object> params = new HashMap<>();
        params.put("loginId", userId);
        params.put("gameId", gameId);
        params.put("uid", uid);
        return companionService.userList(params, order, page, size);
    }


    @ApiOperation(value = "用户端-陪玩订单", tags = {"陪玩"}, notes = "用户端-陪玩订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",required = false, value = "订单id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "state", required = false,value = "订单状态", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/order/list/user", name = "用户端-陪玩订单")
    public Result orderListUser(
            @RequestParam(defaultValue = "") Long orderId,
            @RequestParam(defaultValue = "") Long state,
            @RequestParam(required = false) Long startTime,
            @RequestParam(required = false) Long endTime,
            @RequestParam(defaultValue = "true") boolean isme,
            @RequestParam(defaultValue = "{}") String order,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", orderId);
        if(isme){
            params.put("companionId", userId);
        }else {
            params.put("userId", userId);
        }
        params.put("state", state);
        params.put("startTime", startTime);
        params.put("endTime", endTime);

        return companionService.orderListUser(params, order, page, size);
    }


    @ApiOperation(value = "管理端-陪玩订单列表", tags = {"陪玩"}, notes = "管理端-陪玩订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companionId",required = false, value = "陪玩id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "orderId",required = false, value = "订单id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "userId",required = false, value = "下单用户", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "state", required = false,value = "订单状态", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "startTime", required = false,value = "开始时间", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "endTime", required = false,value = "结束时间", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "gameCategory", required = false,value = "游戏类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/order/list", name = "管理端-陪玩订单列表")
    public Result orderList(
            @RequestParam(defaultValue = "") Long companionId,
            @RequestParam(defaultValue = "") Long orderId,
            @RequestParam(defaultValue = "") Long userId,
            @RequestParam(defaultValue = "") Long state,
            @RequestParam(defaultValue = "") Long startTime,
            @RequestParam(defaultValue = "") Long endTime,
            @RequestParam(defaultValue = "") String gameCategory,
            @RequestParam(defaultValue = "{}") String order,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size, Authentication authentication) {
//        Long userId = getAuthUser(authentication).getId();
        Map<String, Object> params = new HashMap<>();
        params.put("companionId", companionId);
        params.put("orderId", orderId);
        params.put("userId", userId);
        params.put("state", state);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("gameCategory", gameCategory);
        return companionService.orderListUser(params, order, page, size);
    }


    @ApiOperation(value = "获取陪玩评论", tags = {"陪玩"}, notes = "获取陪玩评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companionId", value = "陪玩用户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/review/list", name = "获取陪玩评论")
    public Result reviewList(
            @RequestParam Long companionId,
            @RequestParam(defaultValue = "{}") String order,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size, Authentication authentication) {
//        Long userId = getAuthUser(authentication).getId();
        Map<String, Object> params = new HashMap<>();
        params.put("companionId", companionId);
        return companionService.reviewList(params, order, page, size);
    }


    @PostMapping(value = "/apply/list", name = "陪玩申请列表")
    @ApiOperation(value = "陪玩申请列表", tags = {"陪玩"}, notes = "陪玩申请列表")
    public Result applyList(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "{}") String order,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            Authentication authentication) {
//        Long userId = getAuthUser(authentication).getId();
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("examine",1);
        return companionGameService.list(params, order, page, size);
    }

    @PostMapping(value = "/ban", name = "陪玩申请列表")
    @ApiOperation(value = "陪玩申请列表", tags = {"陪玩"}, notes = "陪玩申请列表")
    public Result ban(Boolean ban,Long companionId) {
        return companionService.ban(companionId, ban);
    }

}
